package com.bw.fxzk

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.SPUtils
import com.bw.base.net.mvvm.BaseViewActivity
import com.bw.fxzk.databinding.ActivityMainBinding
import com.bw.fxzk.entity.MyTabEntity
import com.bw.fxzk.fg.AllFragment
import com.bw.fxzk.fg.ChangeFragment
import com.bw.fxzk.fg.SignFragment
import com.bw.fxzk.room.AppDBUtlis
import com.bw.fxzk.room.Score
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.google.android.material.chip.Chip
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import java.text.SimpleDateFormat

class MainActivity : BaseViewActivity<ActivityMainBinding>() {
    lateinit var vpAdapter: VpAdapter
    val fraglist=mutableListOf<Fragment>()
    val tabs= arrayListOf<CustomTabEntity>()
    lateinit var allFragment: AllFragment
    lateinit var signFragment: SignFragment
    lateinit var changeFragment: ChangeFragment
    val set=mutableSetOf<String>()
    override fun initView() {
        //流式布局
//        val stringSet = SPUtils.getInstance().getStringSet("keyword")
//        if(stringSet.size>0) {
//            set.addAll(stringSet)
//            queryName()
//        }

//        binding.add.setOnClickListener {
//            set.add(binding.et.text.toString())
//            binding.et.setText("")
//            queryName()
//        }


        allFragment= AllFragment()
        signFragment= SignFragment()
        changeFragment= ChangeFragment()
        fraglist.add(allFragment)
        fraglist.add(signFragment)
        fraglist.add(changeFragment)
        vpAdapter= VpAdapter(this,fraglist)
        binding.vp.adapter=vpAdapter
        binding.vp.offscreenPageLimit=2
        tabs.add(MyTabEntity("全部",0,0))
        tabs.add(MyTabEntity("收入",0,0))
        tabs.add(MyTabEntity("支出",0,0))
        binding.ctl.setTabData(tabs)
        binding.ctl.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                binding.vp.currentItem=position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        binding.vp.registerOnPageChangeCallback(object :OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.ctl.currentTab=position
            }
        })
        binding.sign.setOnClickListener {
            val sdf=SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val format = sdf.format(System.currentTimeMillis())
            AppDBUtlis.getDB().getScoreDao().insert(Score(0,46,0,format))
            queryAll()
            allFragment.queryAll()
            signFragment.queryAll()
            changeFragment.queryAll()


        }
        queryAll()
        binding.change.setOnClickListener {
            val sdf=SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val format = sdf.format(System.currentTimeMillis())
            AppDBUtlis.getDB().getScoreDao().insert(Score(0,-100,1,format))
            queryAll()
            allFragment.queryAll()
            signFragment.queryAll()
            changeFragment.queryAll()
        }
    }

//    private fun queryName() {
//        if (set.size>0){
//            binding.cg.removeAllViews()
//            set.forEach {
//                val chip = LayoutInflater.from(this).inflate(R.layout.item_chip, null) as Chip
//                chip.findViewById<TextView>(R.id.name).text=it
//                binding.cg.addView(chip)
//                chip.setOnClickListener { it1->
//                    binding.et.setText(it)
//                }
//                chip.setOnCloseIconClickListener { it2->
//                    set.remove(it)
//                    binding.cg.removeView(chip)
//                    SPUtils.getInstance().put("keyword",set)
//                }
//            }
//            SPUtils.getInstance().put("keyword",set)
//        }
//    }

    private fun queryAll() {
        var total=0
        var totalSign=0
        var totalChange=0
        val queryAll = AppDBUtlis.getDB().getScoreDao().queryAll()
        if (queryAll.isNotEmpty()){
            queryAll.forEach {
                total+=it.num

            }
        }
        binding.total.text="当前积分：$total 分"
        val querySign = AppDBUtlis.getDB().getScoreDao().queryType(0)
        if (querySign.isNotEmpty()){
            querySign.forEach {
                totalSign+=it.num
            }
        }
        binding.totalSign.text="收入：$totalSign 分"
        val queryChange = AppDBUtlis.getDB().getScoreDao().queryType(1)
        if (queryChange.isNotEmpty()){
            queryChange.forEach {
                totalChange+=it.num
            }
        }
        binding.totalChange.text="支出：${0-totalChange} 分"

    }
    override fun initData() {

    }

    override fun getLayoutId(): Int =  R.layout.activity_main
    override fun onResume() {
        super.onResume()
        queryAll()
    }
}